﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Business.COMM;
using Mang24k99.Model.COMM;
using Mang24k99.Model.GOLD;
using Mang24k99.Business.GOLD;

namespace Mang24k99.Web.ProductQuote
{
    public partial class OrglistProductQuote : System.Web.UI.Page
    {
        //产品归属类别：1-金商，2-银行
        protected string product_type = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["procduct_type"] != null)
            {
                product_type = Request.QueryString["procduct_type"].ToString();
                if (product_type == "1")
                {
                    this.sort.Visible = false;
                }
            }
            if (!IsPostBack)
            {
                BindOrglist();
                BindGoldType();
                HideColumns();
                BindGridView();
            }
        }
        /// <summary>
        /// 根据不同的类别隐藏列
        /// </summary>
        public void HideColumns()
        {
            if (ddlGoldType.SelectedValue=="")
            {
                string strMsg = "金品品种为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                this.Panel2.Visible = false;
                return;
            }
            string  goldtype=ddlGoldType.SelectedValue;
            if (product_type=="1")
            {
                //金商：黄金、铂金、钯金
                if (goldtype == "30201" || goldtype == "30202" || goldtype == "30203")
                {
                    this.Panel2.Visible = true;
                    this.grdList.Visible = true;
                    grdList.Columns[2].HeaderText = "金品名称";
                    grdList.Columns[3].Visible = false;
                    grdList.Columns[3].HeaderText = "重量";
                    grdList.Columns[4].Visible = false;
                    grdList.Columns[5].Visible = true;
                    grdList.Columns[5].HeaderText = "卖出价";
                    grdList.Columns[6].Visible = true;
                    grdList.Columns[6].HeaderText = "回收价";
                    grdList.Columns[7].Visible = false;
                    grdList.Columns[8].Visible = false;
                    grdList.Columns[9].Visible = false;
                }
                //金商：金条、金币、银条
                if (goldtype == "30206" || goldtype == "30204" || goldtype == "30205" )
                {
                    this.Panel2.Visible = true;
                    this.grdList.Visible = true;
                    grdList.Columns[2].HeaderText = "金品名称";
                    grdList.Columns[3].Visible = true;
                    grdList.Columns[3].HeaderText = "重量";
                    grdList.Columns[4].Visible = true;
                    grdList.Columns[6].Visible = false;
                    grdList.Columns[5].Visible = true;
                    grdList.Columns[5].HeaderText = "单价";
                    grdList.Columns[7].Visible = false;
                    grdList.Columns[8].Visible = false;
                    grdList.Columns[9].Visible = true;
                }
            }
            if (product_type=="2")
            {
                //银行：金条、金章
                if (goldtype == "30204" || goldtype == "30207")
                {
                    this.Panel2.Visible = true;
                    this.grdList.Visible = true;
                    grdList.Columns[2].HeaderText = "金品名称";
                    grdList.Columns[3].Visible = true;
                    grdList.Columns[3].HeaderText = "重量";
                    grdList.Columns[4].Visible = true;
                    grdList.Columns[6].Visible = false;
                    grdList.Columns[5].Visible = true;
                    grdList.Columns[5].HeaderText = "单价";
                    grdList.Columns[7].Visible = true;
                    grdList.Columns[8].Visible = true;
                    grdList.Columns[9].Visible = true;
                    
                }
            }
        }
        /// <summary>
        /// 绑定机构
        /// </summary>
        public void BindOrglist()
        {
            string where = string.Empty;
            if (product_type=="1")   //金商机构
            {
                where = " orglist.ParentNO=2 and orglist.IsValidate=1 and orglist.IsColumn=1 order by orglist.LevelOrder";
            }
            else if (product_type=="2")     //银行机构
            {
                where = " orglist.ParentNO=3 and orglist.IsValidate=1 and orglist.IsColumn=1 order by orglist.LevelOrder";
            }
            MKtCommOrglist[] orglist = (MKtCommOrglist[])BKtCommOrglist.SelectByWhere(where);
            this.ddlOrglist.DataSource = orglist;
            this.ddlOrglist.DataTextField = "OrgnizeName";
            this.ddlOrglist.DataValueField = "OrgnizeNO";
            this.ddlOrglist.DataBind();           
        }

        /// <summary>
        /// 绑定金品分类
        /// </summary>
        public void BindGoldType()
        {
            MKtCommCodeList[] codelist = (MKtCommCodeList[])BKtGoldGoldbreed.SelectGoldTypeByBelong(Convert.ToInt32(product_type));
            this.ddlGoldType.DataSource = codelist;
            this.ddlGoldType.DataTextField = "CODE_CNAME";
            this.ddlGoldType.DataValueField = "CODE_NO";
            this.ddlGoldType.DataBind();
        }

        /// <summary>
        /// 绑定gridview
        /// </summary>
        public void BindGridView()
        {
            HideColumns();
            if (ddlGoldType.SelectedValue=="")
            {
                string strMsg = "金品品种为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return;
            }
            if (ddlOrglist.SelectedValue=="")
            {
                string strMsg = "机构名称为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return;
            }
            int orglistno = Convert.ToInt32(this.ddlOrglist.SelectedValue);
            int goldtype = Convert.ToInt32(this.ddlGoldType.SelectedValue);
            MKtGoldProduct[] goldproductlist = (MKtGoldProduct[])BKtGoldProduct.SelectByWhere(orglistno, goldtype);
            if (goldproductlist.Length==0)
            {
                this.Panel2.Visible = false;
            }
            if (product_type=="2")
            {
                for (int i = 0; i < goldproductlist.Length; i++)
                {
                    DateTime lastpriceChangeTime = (DateTime)goldproductlist[i].MKtGoldSaleprice.PriceDatetime;
                    DateTime datetimenow = DateTime.Now;
                    //当报价日期和当前日期不一样时，让页面上次卖出报价显示为卖出价（正确自动计算涨跌）
                    if (lastpriceChangeTime.ToString("yyyyMMdd") != datetimenow.ToString("yyyyMMdd"))
                    {
                        goldproductlist[i].MKtGoldSaleprice.LastsalePrice = goldproductlist[i].MKtGoldSaleprice.SalePrice;
                    }
                }
            }
           
            
            this.grdList.DataSource = goldproductlist;
            this.grdList.DataBind();
        }
        protected void ddlGoldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            MKtGoldProduct goldproduct = new MKtGoldProduct();
            HideColumns();
            BindGridView();
        }

        protected void ddlOrglist_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int count = this.grdList.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                int NO = Int32.Parse(this.grdList.Rows[i].Cells[1].Text);
                MKtGoldSaleprice saleprice = BKtGoldSaleprice.Find(NO);
                GridViewRow gvr = grdList.Rows[i];
                TextBox txtBuyPrice = (TextBox)gvr.FindControl("txtBuyPrice");
                TextBox txtSalePrice = (TextBox)gvr.FindControl("txtSalePrice");
                TextBox txtChargeFee = (TextBox)gvr.FindControl("txtChargeFree");
                TextBox txtTotalPrice = (TextBox)gvr.FindControl("txtTotalPrice");
                TextBox txtPriceChange = (TextBox)gvr.FindControl("txtPriceChange");
                string goldtype = ddlGoldType.SelectedValue;
                if (product_type == "1")
                {
                    //金商：黄金、铂金、钯金
                    if (goldtype == "30201" || goldtype == "30202" || goldtype == "30203")
                    {
                        if (txtBuyPrice.Text.Trim() != "" && txtBuyPrice.Text.Trim() != "--")
                        {
                            if (Common.PageValidate.IsDecimal(txtBuyPrice.Text.Trim()) == false)
                            {
                                string strMsg = "回收价格式不对，请输入正确的小数格式，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            if (Convert.ToDecimal(txtBuyPrice.Text.Trim()) > 999999)
                            {
                                string strMsg = "回收价输入数字过大，请将数字整数位限制在6位以内，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            if (Convert.ToDecimal(txtBuyPrice.Text.Trim()) == 0)
                            {
                                string strMsg = "回收价不能为0，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            saleprice.BuyPrice = Convert.ToDecimal(txtBuyPrice.Text.Trim());
                        }
                        else
                        {
                            saleprice.BuyPrice = 0;
                        }
                        if (txtSalePrice.Text.Trim() != "" && txtSalePrice.Text.Trim() != "--")
                        {
                            if (Common.PageValidate.IsDecimal(txtSalePrice.Text.Trim()) == false)
                            {
                                string strMsg = "卖出价格式不对，请输入正确的小数格式，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            if (Convert.ToDecimal(txtSalePrice.Text.Trim()) > 999999)
                            {
                                string strMsg = "卖出价输入数字过大，请将数字整数位限制在6位以内，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            if (Convert.ToDecimal(txtSalePrice.Text.Trim()) == 0)
                            {
                                string strMsg = "卖出价不能为0，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            saleprice.SalePrice = Convert.ToDecimal(txtSalePrice.Text.Trim());
                        }
                        else
                        {
                            saleprice.SalePrice = 0;
                        }
                    }
                     //金商：金条、金币、银条
                    if (goldtype == "30204" || goldtype == "30205" || goldtype == "30206")
                    {
                        if (txtSalePrice.Text.Trim() != "" && txtSalePrice.Text.Trim() != "--")
                        {
                            if (Common.PageValidate.IsDecimal(txtSalePrice.Text.Trim()) == false)
                            {
                                string strMsg = "单价格式不对，请输入正确的小数格式，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            if (Convert.ToDecimal(txtSalePrice.Text.Trim()) > 999999)
                            {
                                string strMsg = "单价输入数字过大，请将数字整数位限制在6位以内，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            if (Convert.ToDecimal(txtSalePrice.Text.Trim()) == 0)
                            {
                                string strMsg = "单价不能为0，位置第" + (i + 1) + "行！";
                                Common.MessageBox.Show(this.Page, strMsg);
                                return;
                            }
                            saleprice.TotalPrice = Convert.ToDecimal(txtTotalPrice.Text);
                            saleprice.SalePrice = Convert.ToDecimal(txtSalePrice.Text);
                        }
                        else
                        {
                            saleprice.TotalPrice = 0;
                            saleprice.SalePrice = 0;
                        }
                    }
                }
                else if (product_type == "2")
                {//验证单价格式
                    if (txtSalePrice.Text.Trim()=="")
                    {
                        string strMsg = "单价不能为空，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    if (Common.PageValidate.IsDecimal(txtSalePrice.Text.Trim()) == false)
                    {
                        string strMsg = "单价格式不对，请输入正确的小数格式，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    if (Convert.ToDecimal(txtSalePrice.Text.Trim()) > 999999)
                    {
                        string strMsg = "单价输入数字过大，请将数字整数位限制在6位以内，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    if (Convert.ToDecimal(txtSalePrice.Text.Trim()) == 0)
                    {
                        string strMsg = "单价不能为0，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    //验证涨跌格式
                    if (txtPriceChange.Text.Trim() == "")
                    {
                        string strMsg = "涨跌不能为空，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    if (Common.PageValidate.IsDecimalSign(txtPriceChange.Text.Trim()) == false)
                    {
                        string strMsg = "涨跌格式不对，请输入正确的小数格式，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    if (Convert.ToDecimal(txtPriceChange.Text.Trim()) > 999999)
                    {
                        string strMsg = "涨跌输入数字过大，请将数字整数位限制在6位以内，位置第" + (i + 1) + "行！";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return;
                    }
                    DateTime lastpriceChangeTime = (DateTime)saleprice.PriceDatetime;
                    DateTime datetimenow = DateTime.Now;
                    //当报价日期和当前日期不一样时，更新上次卖出报价为卖出价
                    if (lastpriceChangeTime.ToString("yyyyMMdd") != datetimenow.ToString("yyyyMMdd"))
                    {
                        saleprice.LastsalePrice = saleprice.SalePrice;
                        MKtGoldSaleprice.OnEditToHistory(saleprice);
                    }
                    saleprice.TotalPrice = Convert.ToDecimal(txtTotalPrice.Text);
                    saleprice.SalePrice = Convert.ToDecimal(txtSalePrice.Text);
                    saleprice.PriceChange = Convert.ToDecimal(txtPriceChange.Text);
                    saleprice.ChargeFee = txtChargeFee.Text;
                }
               
                saleprice.PriceDatetime = DateTime.Now;
                saleprice.UpdateTime = DateTime.Now;
                saleprice.IsValidate = 1;
               
                try
                {
                    saleprice.Update();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
            string Msg = "报价更新成功！";
            Common.MessageBox.Show(this.Page, Msg);
            BindGridView();
        }
        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void grdList_DataBinding(object sender, EventArgs e)
        {
            
        }

        protected void grdList_PreRender(object sender, EventArgs e)
        {
            Common.PageOp.SetRepeaterAttributes(grdList.Rows, "txtTotalPrice", "readonly", "readonly");
            
        }
        public string Change(int tag,decimal num)
        {
            if (tag==1)
            {
                if (num == 0)
                {
                    return "--";
                }
                else
                {
                    return Convert.ToDouble(num).ToString("0.####");
                }
            }
            if (tag==2)
            {
                return Convert.ToDouble(num).ToString("0.####");
            }
            return "--";
        }
    }
}
